package com.neuedu.autosale.sys.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.neuedu.autosale.entity.FileResult;
import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;
import com.neuedu.autosale.sys.entity.Brand;
import com.neuedu.autosale.sys.service.BrandService;
import com.neuedu.autosale.util.FileuploadUtil;

@Controller
public class BrandController {
	@Autowired
	private BrandService brandService;

	@RequestMapping("brand/index")
	public String brandIndex() {
		return "car/brandlist";
	}
	
	@RequestMapping("brand/findall")
	@ResponseBody
	public List<Brand> brandFindall(){
		return brandService.findAll();
	}

	@RequestMapping("brand/list")
	@ResponseBody
	public Pager<Brand> brandList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows,HttpServletRequest request) {
		String brandString = request.getParameter("brandName");
		Brand brandName = new Brand(brandString);
//		Pager<Brand> pager = new Pager<>(page, rows);
		Pager<Brand> pager = new Pager<>(page, rows, brandName);
		List<Brand> brandlist = brandService.findListByPager(pager);
		int total = brandService.findTotalByPager(pager);
		pager.setRows(brandlist);
		pager.setTotal(total);
		return pager;
	}

	@RequestMapping("brand/add")
	@ResponseBody
	public Result doAdd(MultipartFile imageFile, Brand brand) {
		if (imageFile != null && !imageFile.isEmpty()) {
			FileResult fileResult = FileuploadUtil.uploadFile(imageFile);
			if (!fileResult.getSuccess()) {
				return new Result(false, fileResult.getMsg());
			}
			String serverFile = fileResult.getServerPath();
			brand.setBrandPhoto(serverFile);
		}
//		String serverFile = uploadFile(imageFile);
//		brand.setBrandPhoto(serverFile);
		Result result = brandService.add(brand);
		return result;
	}
	
	@RequestMapping("brand/edit")
	@ResponseBody
	public Result doEdit(MultipartFile imageFile, Brand brand) {
		
		if (imageFile != null && !imageFile.isEmpty()) {
			FileResult fileResult = FileuploadUtil.uploadFile(imageFile);
			if (!fileResult.getSuccess()) {
				return new Result(false, fileResult.getMsg());
			}
			String serverFile = fileResult.getServerPath();
			brand.setBrandPhoto(serverFile);
		}
		
		Result result = brandService.edit(brand);
		return result;
	}
	
	@RequestMapping("brand/delete")
	@ResponseBody
	public Result doDelete(Integer brandId) {
		List<Brand> list = new ArrayList<Brand>();
		list.add(new Brand(brandId));
		Result result = brandService.deleteByIds(list);
		return result;
	}
	

	private String uploadFile(MultipartFile imageFile) {
		if (imageFile != null && !imageFile.isEmpty()) {
			String path = "D:/ftp/uploads/images";
			File pathFile = new File(path);
			pathFile.mkdirs();
			String FileName = imageFile.getOriginalFilename();
			File outFile = new File(pathFile, FileName);
			try {
				imageFile.transferTo(outFile);
				return "images/" + FileName;
			} catch (IllegalStateException e) {
			} catch (IOException e) {
			}
		}
		return null;
	}

}
